<template>
  <el-dialog title="角色添加" :visible="visible" :show-close="false" @close="$emit('update:visible', false)">
    <el-form ref="roleForm" :model="form" :rules="validateRules()" label-width="120px">
      <el-form-item label="角色名称" prop="name">
        <el-input ref="roleNameRef" v-model="form.name" autocomplete="off" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('update:visible', false)">取 消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">{{ form.id ? '修改角色' : '添加角色' }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { errorMsg, successMsg } from '@/utils/msg'
import { checkRoleExistsApi, addRoleApi } from '@/api/roleApi'
import _ from 'lodash'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      loading: false,
      form: {
        name: ''
      }
    }
  },
  methods: {
    // 点击添加用户后
    doSubmit() {
      this.loading = true
      this.$refs['roleForm'].validate(async valid => {
        // 前端表单验证通过
        if (valid) {
          let ret = await addRoleApi(this.form)
          if (ret.code == 0) {
            successMsg('添加角色成功', () => {
              this.loading = false
              this.$parent.loadData().then(() => {
                this.$emit('update:visible', false)
                this.form.name = ''
              })
            })
          } else {
            errorMsg('添加角色失败', () => {
              this.loading = false
            })
          }
        }
      })
    },
    validateRules() {
      return {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' },
          { validator: this.checkRoleName, trigger: 'blur' }
        ]
      }
    },
    // 验证角色是否已经存在,如果存在则提示用户
    // async checkRoleName(rule, value, callback) {
    //     let ret = await checkRoleExistsApi(value)
    //     console.log(ret)
    //     if (ret) {
    //       callback()
    //     } else {
    //       callback(new Error('角色已经存在'))
    //     }
    // }
    checkRoleName(rule, value, callback) {
      // const fn = _.debounce(function (callback) {
      //   checkRoleExistsApi(value).then(ret => {
      //     if (!ret) {
      //       callback()
      //     } else {
      //       callback(new Error('角色已经存在'))
      //     }
      //   })
      // }, 1000)
      // const fn = callback => {
      //   checkRoleExistsApi(value).then(ret => {
      //     if (!ret) {
      //       callback()
      //     } else {
      //       callback(new Error('角色已经存在'))
      //     }
      //   })
      // }
      // fn(callback)
      // el-input__inner
      this.$refs['roleNameRef'].$el.querySelector('.el-input__inner').onblur = function () {
        checkRoleExistsApi(value).then(ret => {
          if (!ret) {
            callback()
          } else {
            callback(new Error('角色已经存在'))
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>
